<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\TeacherGetPriceModel;
use app\model\TeacherRevenueHistoryModel;
use app\model\UserModel;
use app\model\UserSuggestModel;
use think\Db;
use think\Request;
use think\Validate;

class User extends Base
{
    /**
     * 用户反馈意见列表页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function reply()
    {
        return view();
    }

    /**
     * 获取用户反馈建议数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function replyList()
    {
        $param = $this->request->param();
        $arr = ['like'=>'user_info.ui_username'];
        $where = $this->select_param($arr,$param);
        $where['t.delete_flag'] = 1;
        $UserSuggestModel = new UserSuggestModel();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $list = $UserSuggestModel->getSuggestListPage($page,$pageSize,$where,$is_count = false,$order='');
        $count = $UserSuggestModel->getSuggestListPage($page,$pageSize,$where,$is_count = true,$order='');
        foreach ($list as $key => $value){
            $list[$key]['us_created_time'] = date('Y-m-d H:i:s',$list[$key]['us_created_time']);
        }
        return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$list];
    }

    /**
     * 处理用户反馈意见
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delFeedback()
    {
        $param = $this->request->param();
        $res = Db::name('user_suggest')->where(['us_id'=>$param['us_id']])->update(['status'=>2]);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }

    /**************************************************/

    /**
     * 用户数据列表页面
     * @return \think\response\View
     */
    public function userIndex()
    {
        return $this->fetch('user/userIndex');
    }

    /**
     * 获取用户列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserList()
    {
        $param = $this->request->param();
        $arr = ['like'=>'user.ui_username','1'=>'user.ui_phone'];
        $where = $this->select_param($arr,$param);
        $where['user.delete_flag'] = 1;
        if (!empty($param['role_id'])) {
            $where['ui_role'] = $param['role_id'];
        }else{
            $where['ui_role'] = 1;
        }
        $UserModel = new UserModel();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $list = $UserModel->getUserListPage($page,$pageSize,$where,$is_count = false,$order='');
        $count = $UserModel->getUserListPage($page,$pageSize,$where,$is_count = true,$order='');
        foreach ($list as $key => $value){

        }
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }


    public function info()
    {
        $param = $this->request->param();
        $user = Db::table('user_info')->where('ui_id',$param['ui_id'])->find();
        if($user['ui_role'] == 2){

        }else{
            $this->redirect('/admin/user/teacherInfo',['ui_id'=>$param['ui_id']]);
        }
    }

    public function teacherInfo()
    {
        $param = $this->request->param();
        $url = url("User/teacherInfoList",['ui_id'=>$param['ui_id']]);
        $this->assign('url',$url);
        $url1 = url("User/getTeacherHistory",['ui_id'=>$param['ui_id']]);
        $this->assign('url1',$url1);
        return $this->fetch('user/teacherInfo');
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function teacherInfoList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $TeacherGetPriceModel = new TeacherGetPriceModel();
        $where = [];
        $list = $TeacherGetPriceModel->getPriceListPage($page,$pageSize,$where,$is_count = false,$order='');
        $count = $TeacherGetPriceModel->getPriceListPage($page,$pageSize,$where,$is_count = true,$order='');
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    public function getTeacherHistory()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $TeacherRevenueHistoryModel = new TeacherRevenueHistoryModel();
        $where = [];
        $list = $TeacherRevenueHistoryModel->getHistoryListPage($page,$pageSize,$where,$is_count = false,$order='');
        $count = $TeacherRevenueHistoryModel->getHistoryListPage($page,$pageSize,$where,$is_count = true,$order='');
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    public function studentInfo()
    {

    }
}